package cn.threewaters.qtyy.tools.autocode.ui;

import cn.threewaters.qtyy.tools.autocode.common.DBConnection;
import cn.threewaters.qtyy.tools.autocode.common.DBModel;
import cn.threewaters.qtyy.tools.autocode.util.Config;
import cn.threewaters.qtyy.tools.autocode.util.DBTool;
import cn.threewaters.qtyy.tools.autocode.util.FrameTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@SuppressWarnings(value = { "rawtypes", "unchecked" })
public class MainFrame extends JFrame implements ActionListener {
	private static final long serialVersionUID = -2804433595966436384L;

	JPanel panel = new JPanel();

	JLabel dbType_lab = new JLabel("数据库类型："), dbUrl_lab = new JLabel("数据库URL："), dbName_lab = new JLabel("数据库名字："),
			userName_lab = new JLabel("用户名："), password_lab = new JLabel("密码：");
	JTextField dbUrl_txt = new JTextField();
	JTextField dbName_txt = new JTextField();
	JTextField userName_txt = new JTextField();
	JPasswordField password_txt = new JPasswordField();
	JComboBox dbType = new JComboBox();

	JButton buttonTest = new JButton("测试连接");
	JButton button1 = new JButton("确定"), button2 = new JButton("退出");

	public MainFrame() {
		panel.setLayout(null);
		panel.setBorder(BorderFactory.createTitledBorder("请输入下面相关信息："));

		dbType_lab.setBounds(20, 40, 120, 30);
		dbUrl_lab.setBounds(20, 90, 120, 30);
		dbName_lab.setBounds(20, 140, 120, 30);
		userName_lab.setBounds(20, 190, 120, 30);
		password_lab.setBounds(20, 240, 120, 30);

		dbType.setBounds(115, 40, 350, 30);
		dbType.addItem(DBConnection.MYSQL_FLAG);

		dbUrl_txt.setBounds(115, 90, 350, 30);
		dbUrl_txt.setText("jdbc:mysql://localhost:3306/");

		dbName_txt.setBounds(115, 140, 350, 30);

		userName_txt.setBounds(115, 190, 350, 30);

		password_txt.setBounds(115, 240, 350, 30);

		buttonTest.setBounds(370, 280, 100, 30);
		button1.setBounds(130, 320, 100, 30);
		button2.setBounds(250, 320, 100, 30);

		Properties config = Config.read();
		if (config != null) {
			dbUrl_txt.setText((String) config.get("dbUrl"));
			dbName_txt.setText((String) config.get("dbName"));
			userName_txt.setText((String) config.get("dbUser"));
			password_txt.setText((String) config.get("dbPwd"));
		}

		panel.add(dbUrl_lab);
		panel.add(dbName_lab);
		panel.add(userName_lab);
		panel.add(password_lab);
		panel.add(dbUrl_txt);
		panel.add(dbName_txt);
		panel.add(userName_txt);
		panel.add(password_txt);
		panel.add(dbType_lab);
		panel.add(dbType);
		panel.add(button1);
		panel.add(button2);
		panel.add(buttonTest);

		button1.addActionListener(this);
		button2.addActionListener(this);
		buttonTest.addActionListener(this);

		add(panel, BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonTest) {
			String db_url = dbUrl_txt.getText().trim();
			String db_name = dbName_txt.getText().trim();
			String db_Type = dbType.getSelectedItem().toString();
			String userName = userName_txt.getText().trim();
			String password = new String(password_txt.getPassword()).trim();
			if (!"".equals(userName) && !"".equals(password)) {
				DBModel model = new DBModel();
				StringBuffer sqlUrl = new StringBuffer();
				sqlUrl.append(db_url);
				if (db_url.substring(db_url.length() - 1, db_url.length()).equals("/")) {
					sqlUrl.append(db_name);
				} else {
					sqlUrl.append("/").append(db_name);
				}
				sqlUrl.append("?characterEncoding=UTF-8");
				model.setDBurl(sqlUrl.toString());
				model.setDBtype(db_Type);
				model.setDBuser(userName);
				model.setDBpwd(password);

				try {
					DBTool.getDBCon(model);
					JOptionPane.showMessageDialog(null, "数据库连接成功", "Genetor提醒您", JOptionPane.INFORMATION_MESSAGE);
				} catch (SQLException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "数据库连接异常，错误信息：" + e1, "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
				} catch (ClassNotFoundException e1) {
					JOptionPane.showMessageDialog(null, "系统出现异常..." + e1, "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				}
			} else if ("".equals(userName)) {
				JOptionPane.showMessageDialog(null, "请输入用户名", "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "用户名密码不允许为空！！", "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
			}
		}
		if (e.getSource() == button1) {
			String db_url = dbUrl_txt.getText().trim();
			String db_name = dbName_txt.getText().trim();
			String db_Type = dbType.getSelectedItem().toString();
			String userName = userName_txt.getText().trim();
			String password = new String(password_txt.getPassword()).trim();
			if (!"".equals(userName) && !"".equals(password)) {
				DBModel model = new DBModel();
				StringBuffer sqlUrl = new StringBuffer();
				sqlUrl.append(db_url);
				if (db_url.substring(db_url.length() - 1, db_url.length()).equals("/")) {
					sqlUrl.append(db_name);
				} else {
					sqlUrl.append("/").append(db_name);
				}
				sqlUrl.append("?characterEncoding=UTF-8");
				model.setDBurl(sqlUrl.toString());
				model.setDBtype(db_Type);
				model.setDBuser(userName);
				model.setDBpwd(password);

				try {
					Connection conn = DBTool.getDBCon(model);
					Map paramMap = new HashMap<String, String>();
					paramMap.put("dbUrl", db_url);
					paramMap.put("dbName", db_name);
					paramMap.put("dbPwd", password);
					paramMap.put("dbUser", userName);
					Config.write(paramMap);
					showTableFrame(conn, model);
					this.setVisible(false);
				} catch (SQLException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "数据库连接异常，错误信息：" + e1, "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
				} catch (ClassNotFoundException e1) {
					JOptionPane.showMessageDialog(null, "系统出现异常..." + e1, "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				}

			} else if ("".equals(userName)) {
				JOptionPane.showMessageDialog(null, "请输入用户名", "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "用户名密码不允许为空！！", "Genetor提醒您", JOptionPane.ERROR_MESSAGE);
			}
		}
		if (e.getSource() == button2)
			System.exit(0);
	}

	public void showTableFrame(Connection conn, DBModel model) {
		TableFrame tableF = new TableFrame(conn, model);
		tableF.setResizable(false);
		tableF.setSize(700, 500);
		FrameTool.setCenter(tableF);
		tableF.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
		tableF.setTitle("Genetor v1.0");
		tableF.setVisible(true);
	}
}
